package cn.itcast.oa.view.action;


import java.util.HashSet;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.domain.User;
import cn.itcast.oa.util.DepartmentUtil;
import cn.itcast.oa.util.MD5Util;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User>{
	

	private Long departmentId ;
	private Long[] roleIds ;
	
	//列表显示
	public String list() throws Exception {
		List<User> userList = userService.findAll();
		ActionContext.getContext().put("userList", userList);
		return "list";
	}
	//增加页面
	public String addUI() throws Exception {
		//准备部门列表
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentlist", departmentList);
		//准备数据
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		
		return "saveUI";
	}
	//增加
	public String add() throws Exception {
		//封装到对象中(当model时实体类型时也可以使用model,但要设置未封装的属性，)
		//---->设置所属部门
		model.setDepartment(departmentService.getById(departmentId));
		//---->设置关联的岗位
		List<Role> roleList = roleService.getByIds(roleIds);
		model.setRoles(new HashSet<Role>(roleList));
		
		//----->设置默认密码为1234(调用MD5Util进行加密)
		String password = MD5Util.md5Encode("1234");
		model.setPassword(password);
		//保存到数据库中
		userService.save(model);
		return "toList";
	}
	//删除信息
	public String delete() throws Exception {
		userService.delete(model.getId());
		return "toList";
	}
	//修改页面
	public String editUI() throws Exception {
		//准备部门列表
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentlist", departmentList);
		//准备数据
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		//回显数据
		User user = userService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(user);
		if (user.getDepartment() != null) {
			departmentId = user.getDepartment().getId();
		}
		if (user.getRoles() != null) {
			roleIds = new Long[user.getRoles().size()] ;
			int index = 0 ;
			for(Role role:user.getRoles()){
				roleIds[index++] = role.getId();
				
			}
		}
		return "saveUI";
	}
	//修改
	public String edit() throws Exception {
		//从数据库中取出
		User user = userService.getById(model.getId());
		//重新设置属性
		user.setName(model.getName());
		user.setLoginName(model.getLoginName());
		user.setDescription(model.getDescription());
		user.setEmail(model.getEmail());
		user.setGender(model.getGender());
		user.setPassword(model.getPassword());
		user.setPhoneNumber(model.getPhoneNumber());
		
		//-------------
		user.setDepartment(departmentService.getById(departmentId));
		List<Role> roleList = roleService.getByIds(roleIds);
		user.setRoles(new HashSet<Role>(roleList));
		//更新到数据库中
		userService.update(user);
		return "toList";
	}
	
		//初始化密码
		public String initPassword() throws Exception {
			//从数据库中取出原对象
			User user = userService.getById(model.getId());
			//设置要修改的属性(调用MD5Util)
			String password = MD5Util.md5Encode("1234");
			user.setPassword(password);
			//更新到数据库中
			userService.update(user);
			return "toList";
		}
		
		/**登录页面*/
		public String LoginUI() throws Exception {
			return "loginUI";
		}
		/**登录*/
		public String Login() throws Exception {
			User user = userService.findByLoginNameAndPassword(model.getLoginName(),model.getPassword());
			if (user == null) {
				addFieldError("name", "用户名或者密码有误！");
				return "loginUI" ;
			}else{
				//登录用户
				ActionContext.getContext().getSession().put("user", user);
				return "toIndex";
			}
		}
		/**注销*/
		public String loginout() throws Exception {
			ActionContext.getContext().getSession().remove("user");
			return "loginUI";
		}
		
		
		
		//---------==================
		public Long getDepartmentId() {
			return departmentId;
		}
		public void setDepartmentId(Long departmentId) {
			this.departmentId = departmentId;
		}
		public Long[] getRoleIds() {
			return roleIds;
		}
		public void setRoleIds(Long[] roleIds) {
			this.roleIds = roleIds;
		}

	
}
